package com.weather.business.service.impl;

import com.weather.business.domain.SevenEchars;
import com.weather.business.domain.WeatherData;
import com.weather.business.domain.WeatherTotalType;
import com.weather.business.mapper.WeatherDataMapper;
import com.weather.business.service.IWeatherDataService;
import com.weather.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 城市天气Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-15
 */
@Service
public class WeatherDataServiceImpl implements IWeatherDataService
{
    @Autowired
    private WeatherDataMapper weatherDataMapper;

    /**
     * 查询城市天气
     *
     * @param id 城市天气主键
     * @return 城市天气
     */
    @Override
    public WeatherData selectWeatherDataById(Long id)
    {
        return weatherDataMapper.selectWeatherDataById(id);
    }

    /**
     * 查询城市天气列表
     *
     * @param weatherData 城市天气
     * @return 城市天气
     */
    @Override
    public List<WeatherData> selectWeatherDataList(WeatherData weatherData)
    {
        return weatherDataMapper.selectWeatherDataList(weatherData);
    }

    @Override
    public SevenEchars selectTotalType(WeatherData weatherData) {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 获取当前月的YearMonth对象
        YearMonth yearMonth = YearMonth.from(today);
        weatherData.setCity("呼和浩特");
        // 获取当月的第一天
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        weatherData.setFirstDayOfMonth(firstDayOfMonth);
        System.out.println("本月第一天: " + firstDayOfMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        // 获取当月的最后一天
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
        weatherData.setLastDayOfMonth(lastDayOfMonth);
        System.out.println("本月最后一天: " + lastDayOfMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        List<WeatherTotalType> list = weatherDataMapper.selectTotalType(weatherData);

        List<String> types = new ArrayList<>();
        List<String> numss = new ArrayList<>();

        for (WeatherTotalType weatherTotalType : list) {
            String type = weatherTotalType.getType();
            String nums = weatherTotalType.getNums();
            types.add(type);
            numss.add(nums);
        }

        SevenEchars map = new SevenEchars();
        map.setTem(types);
        map.setDays(numss);

        return map;
    }

    /**
     * 查询城市天气列表
     *
     * @param weatherData 城市天气
     * @return 城市天气
     */
    @Override
    public List<WeatherData> selectWeatherDataAllList(WeatherData weatherData)
    {
        return weatherDataMapper.selectWeatherDataAllList(weatherData);
    }

    /**
     * 新增城市天气
     *
     * @param weatherData 城市天气
     * @return 结果
     */
    @Override
    public int insertWeatherData(WeatherData weatherData)
    {
        return weatherDataMapper.insertWeatherData(weatherData);
    }

    /**
     * 修改城市天气
     *
     * @param weatherData 城市天气
     * @return 结果
     */
    @Override
    public int updateWeatherData(WeatherData weatherData)
    {
        weatherData.setUpdateTime(DateUtils.getNowDate());
        return weatherDataMapper.updateWeatherData(weatherData);
    }

    /**
     * 批量删除城市天气
     *
     * @param ids 需要删除的城市天气主键
     * @return 结果
     */
    @Override
    public int deleteWeatherDataByIds(Long[] ids)
    {
        return weatherDataMapper.deleteWeatherDataByIds(ids);
    }

    /**
     * 删除城市天气信息
     *
     * @param id 城市天气主键
     * @return 结果
     */
    @Override
    public int deleteWeatherDataById(Long id)
    {
        return weatherDataMapper.deleteWeatherDataById(id);
    }

}
